﻿using System;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;

namespace PP.Basic
{
    /// <summary>
    /// 全局层级结构，单例，持续存在，不随场景切换而销毁。
    /// 若场景中已存在则使用，否则从资源中创建。
    /// </summary>
    [AddComponentMenu("")] // Make it be hidden from the Add Component menu
    public class GlobalHierarchy : UnitySingletonWithCreateMethodDontDestroyOnLoad<GlobalHierarchy>
    {
        private const string Tag = nameof(GlobalHierarchy);

        // Public Properties Begin
        /// <summary>
        /// 全局层级结构初始化完成事件
        /// </summary>
        public static event Action Initialized;

        public EventSystem EventSystem => Instance?._eventSystem;

        public static Camera MainCamera => Instance?._mainCamera;

        /// <summary>
        /// 缓存对象层
        /// 用于放置可重复使用的UI对象，如Toast，消息对话框等。
        /// </summary>
        public static RectTransform PoolLayer => (RectTransform)Instance?._poolTrans;
        /// <summary>
        /// 模板层
        /// 用于放置模板界面，如Toast模板、消息对话模板等。
        /// </summary>
        public static RectTransform TemplateLayer => (RectTransform)Instance?._templateTrans;
        /// <summary>
        /// 背景层
        /// 用于放置全局背景图片，背景特效等。
        /// </summary>
        public static RectTransform BackgroundLayer => (RectTransform)Instance?._backgroundTrans;
        /// <summary>
        /// 内容底层
        /// 用于显示较低层级的内容，比如全屏界面，从而确保其它弹窗或新创建的内容显示在它的上面。
        /// </summary>
        public static RectTransform BottomLayer => (RectTransform)Instance?._bottomTrans;
        /// <summary>
        /// 常规内容层
        /// 用于放置内容界面，如主界面、功能界面、游戏界面等。
        /// </summary>
        public static RectTransform ContentLayer => (RectTransform)Instance?._contentTrans;
        /// <summary>
        /// 弹窗层
        /// 用于放置弹窗界面，如提示框、确认框等。
        /// </summary>
        public static RectTransform PopupLayer => (RectTransform)Instance?._popupTrans;
        /// <summary>
        /// 遮挡层
        /// 用于放置遮挡界面，如加载界面、等待界面等。
        /// </summary>
        public static RectTransform OverlayLayer => (RectTransform)Instance?._overlayTrans;
        /// <summary>
        /// 前景层
        /// 用于放置需要显示在最前端的内容。
        /// </summary>
        public static RectTransform ForegroundLayer => (RectTransform)Instance?._foregroundTrans;

        public static Transform PoolTrans => Instance?._poolTrans;
        public static Transform TemplateTrans => Instance?._templateTrans;
        public static Transform BackgroundTrans => Instance?._backgroundTrans;
        public static Transform BottomTrans => Instance?._bottomTrans;
        public static Transform ContentTrans => Instance?._contentTrans;
        public static Transform PopupTrans => Instance?._popupTrans;
        public static Transform OverlayTrans => Instance?._overlayTrans;
        public static Transform ForegroundTrans => Instance?._foregroundTrans;
        /// <summary>
        /// 3D空间世界根节点
        /// </summary>
        public static Transform WorldTrans => Instance?._worldTrans;

        public static bool IsReady => _instance != null;
        // Public Properties End

        // Private Variables Begin
        // Private Variables End

        // Public Static Methods Begin
          /// <summary>
        /// Create a GameObject used to hold pooled objects.
        /// </summary>
        /// <returns></returns>
        public static GameObject CreatePoolContainer(string name = null)
        {
            var go = new GameObject(name);
            var rectTransform = go.AddComponent<RectTransform>();
            rectTransform.SetParent(PoolLayer, false);
            rectTransform.anchorMin = Vector2.zero;
            rectTransform.anchorMax = Vector2.one;
            rectTransform.offsetMin = Vector2.zero;
            rectTransform.offsetMax = Vector2.zero;
            return go;
        }

        // Public Static Methods End

        // Public Methods Begin
        /// <summary>
        /// 全局范围启用UI事件系统，恢复UI交互
        /// </summary>
        public void EnableEventSystem()
        {
            _eventSystem.enabled = true;
        }

        /// <summary>
        /// 全局范围禁用UI事件系统，禁止UI交互
        /// </summary>
        public void DisableEventSystem()
        {
            _eventSystem.enabled = false;
        }
        // Public Methods End

        // Lifecycle Methods Begin
        protected override void OnInit()
        {
            InitHier();

            Initialized?.Invoke();
        }

        protected override void OnDestroy()
        {
            base.OnDestroy();
        }
        // Lifecycle Methods End

        // Hier Components Begin        
        private EventSystem _eventSystem;
        private Camera _mainCamera;
        private Transform _poolTrans;
        private Transform _templateTrans;
        private Transform _backgroundTrans;
        private Transform _bottomTrans;
        private Transform _contentTrans;
        private Transform _popupTrans;
        private Transform _overlayTrans;
        private Transform _foregroundTrans;
        private Transform _worldTrans;

        private void InitHier()
        {
            _eventSystem = transform.Find("_EventSystem").GetComponentEx<EventSystem>();
            _mainCamera = transform.Find("_Camera_Main").GetComponentEx<Camera>();
            _poolTrans = transform.Find("Pool/_Trans_Pool");
            _templateTrans = transform.Find("Template/_Trans_Template");
            _backgroundTrans = transform.Find("Background/_Trans_Background");
            _bottomTrans = transform.Find("Bottom/_Trans_Bottom");
            _contentTrans = transform.Find("Content/_Trans_Content");
            _popupTrans = transform.Find("Popup/_Trans_Popup");
            _overlayTrans = transform.Find("Overlay/_Trans_Overlay");
            _foregroundTrans = transform.Find("Foreground/_Trans_Foreground");
            _worldTrans = transform.Find("_Trans_World");

            SetSafeArea();
        }
        // Hier Components End

        // Private Methods Begin
        private void SetSafeArea()
        {
        }
        // Private Methods End
    }
}
